package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.application.LoveApplication;
import com.love_weinuo.bean.CodeCallbackBean;
import com.love_weinuo.bean.RegisterCallBackBean;
import com.love_weinuo.databinding.ActivityRegisterBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.view.chat.ILoginView;
import com.love_weinuo.ui.view.chat.PojoLoginPresenter;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.MobileInfoUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.timeutil.RegisterTime;

import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator
 * on 2019/2/22.
 */

public class RegisterActivity extends BaseActivity implements ILoginView {

    RegisterTime timeUtil;
    ActivityRegisterBinding binding;
    PojoLoginPresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        mPresenter = new PojoLoginPresenter(this);
        setTitleText(getResources().getString(R.string.register));
        setBack();
        binding.textViewCode.setOnClickListener(this);
        binding.textViewUpdata.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtil != null) {
            timeUtil.onCancel();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewCode:
                final String phone = binding.phone.getText().toString().trim();
                if (!MobileInfoUtil.isPhoneNumber(phone)) {
                    ToastUtil.showToast("请输入您的手机号码");
                    return;
                } else {
                    timeUtil = new RegisterTime(60000, 1000, binding.textViewCode);
                    timeUtil.start();
                    getcode(phone);
                }
                break;
            case R.id.textViewUpdata:
                final String phoneUpdada = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phoneUpdada) || phoneUpdada.length() != 11) {
                    showInCommonDialog("请输入正确格式的手机号码");
                }

                String code = binding.code.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("请输入验证码");
                    return;
                }
                final String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }
                showProgressDialog();

                String registrationId;
                if (TextUtils.isEmpty(LoveApplication.getInstance().getRegistrationId())) {
                    registrationId = JPushInterface.getRegistrationID(this);
                } else {
                    registrationId = LoveApplication.getInstance().getRegistrationId();
                }

                HTTPAPI.getInstance().register(phoneUpdada, pwd, code,
                        registrationId,
                        new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelProgressDialog();
                                LogUtil.e("联网成功" + s);
                                try {
                                    RegisterCallBackBean bean = GsonUtil.GsonToBean(s, RegisterCallBackBean.class);
                                    switch (bean.getErrcode()) {
                                        case 0:
                                            ToastUtil.showToast("注册成功");
                                            SpUtils.put("module_user_id", bean.getResult().getUid() + "");
                                            SpUtils.put("module_user_sign",bean.getResult().getUserusign());
                                            SpUtils.put("module_user_name",phoneUpdada);
                                            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                                            intent.putExtra("module_user_name",phoneUpdada);
                                            intent.putExtra("module_pwd",pwd);
                                            startActivity(intent);
                                            finish();
                                            break;
                                        default:
                                            showInCommonDialog(bean.getMsg());
                                    }
                                } catch (Exception e) {
                                    LogUtil.e("注册解析错误");
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                cancelProgressDialog();
                                showInCommonDialog(e.getMessage());
                            }
                        });
                break;
            default:
        }
    }

    @Override
    public void onLoginSuccess(Object res) {
        ToastUtil.showToast("登录成功");
    }

    @Override
    public void onLoginFail(String module, int errCode, String errMsg) {
        ToastUtil.showToast("登录失败");
        LogUtil.e(module + "  errCode==>" + errCode + "   meg==>" + errMsg);
    }

    @Override
    public void onRegisterSuccess(Object res) {
        ToastUtil.showToast("注册成功");
    }

    @Override
    public void onRegisterFail(String module, int errCode, String errMsg) {
        ToastUtil.showToast("注册失败");
    }

    public void getcode(String phone) {
        HTTPAPI.getInstance().getRegisterCode(phone, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                CodeCallbackBean bean = GsonUtil.GsonToBean(s, CodeCallbackBean.class);
                switch (bean.getErrcode()) {
                    case 0:
                         ToastUtil.showToast(bean.getMsg());
                        break;
                    default:
                         ToastUtil.showToast(bean.getMsg());
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                 ToastUtil.showToast("验证码发送失败");
            }
        });
    }

}
